package com.wanxs.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanxs.mall.common.page.PageData;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.common.utils.Query;
import com.wanxs.mall.product.dao.CategoryBrandRelationDao;
import com.wanxs.mall.product.dto.CategoryBrandRelationDTO;
import com.wanxs.mall.product.entity.BrandEntity;
import com.wanxs.mall.product.entity.CategoryBrandRelationEntity;
import com.wanxs.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@Service
public class CategoryBrandRelationServiceImpl extends CrudServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity, CategoryBrandRelationDTO> implements CategoryBrandRelationService {

    @Override
    public QueryWrapper<CategoryBrandRelationEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();


        String brandId = (String)params.get("brandId");
        if(StringUtils.isNotBlank(brandId)){
            wrapper.eq(StringUtils.isNotBlank(brandId), "brand_id", brandId);
        }

        return wrapper;
    }


    @Override
    public PageData<CategoryBrandRelationEntity> queryCagelogPage(Map<String, Object> params, QueryWrapper<CategoryBrandRelationEntity> queryWrapper) {
        IPage<CategoryBrandRelationEntity> page = baseDao.selectPage(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                queryWrapper
        );

        return getPageData(page,CategoryBrandRelationEntity.class);

    }

    @Override
    public void updateBrandName(Long brandId, String brandName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(brandName);
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategoryName(Long catId, String catName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setBrandName(catName);
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("cat_id",catId));
    }

    @Override
    public List<BrandEntity> queryBrandListByCatId(Long catId) {
        List<CategoryBrandRelationEntity> cateBrandRelationList = baseDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> collect = cateBrandRelationList.stream().map((item -> {
            BrandEntity brandEntity = new BrandEntity();
            brandEntity.setBrandId(item.getBrandId());
            brandEntity.setName(item.getBrandName());
            return brandEntity;
        })).collect(Collectors.toList());
        return collect;
    }

}